﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace PJ.Framework
{
    /// <summary>
    /// PDF Reader 
    /// </summary>
    public class PDFReader
    {
        /// <summary>
        /// Prints a pdf document
        /// </summary>
        /// <param name="pdfData">document</param>
        public static void PrintPdf(byte[] pdfData)
        {
            string tempFile;

            tempFile = Path.GetTempFileName();

            using (FileStream fs = new FileStream(tempFile, FileMode.Create))
            {
                fs.Write(pdfData, 0, pdfData.Length);
                fs.Flush();
            }

            try
            {
                string gsArguments;
                string gsLocation;
                ProcessStartInfo gsProcessInfo;
                Process gsProcess;
                gsArguments = string.Format("/p \"{0}\"", tempFile);
                
                gsLocation = @"C:\Program Files\Foxit Software\Foxit Reader\Foxit Reader.exe";
                if (!System.IO.File.Exists(gsLocation))
                    gsLocation = @"C:\Program Files (x86)\Foxit Software\Foxit Reader\Foxit Reader.exe";
                if (!System.IO.File.Exists(gsLocation))
                    throw new FileNotFoundException("Foxit Reader not found", gsLocation);
                
                gsProcessInfo = new ProcessStartInfo();
                gsProcessInfo.WindowStyle = ProcessWindowStyle.Hidden;
                gsProcessInfo.FileName = gsLocation;
                gsProcessInfo.Arguments = gsArguments;

                gsProcess = Process.Start(gsProcessInfo);
                gsProcess.WaitForExit();
                gsProcess = Process.Start(gsProcessInfo);
                gsProcess.WaitForExit();
            }
            finally
            {
                File.Delete(tempFile);
            }
        }
    }
}
    
